package viettravel.guiandevent.map;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import viettravel.DAO.MoveMapDAO;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import viettravel.guiandevent.map.model.Position;
import android.os.Bundle;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

public class DirectionListActivity extends SherlockActivity {
	ListView listDirection;
	TextView txtStart;
	TextView txtEnd;
	TextView txtVia;
	TextView txtTime;
	TextView txtDistance;
	ArrayList<MoveMapDAO> list;
	ListLocalMapAdapter adaptor;
	Position startPosition;
	Position endPosition;
	String jsonString;
	String elocation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_direction_list);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setIcon(R.drawable.ic_back_arrow);
		actionBar.setBackgroundDrawable(Constants.APP_DRAWABLE);
		actionBar.setTitle(R.string.directionactivity_title);

		ArrayList<MoveMapDAO> list = new ArrayList<MoveMapDAO>();
		txtStart = (TextView) findViewById(R.id.txtMyLocation);
		txtStart.setTypeface(Constants.TYPEFACE_NYALA);
		txtEnd = (TextView) findViewById(R.id.txtDestination);
		txtEnd.setTypeface(Constants.TYPEFACE_NYALA);
		txtDistance = (TextView) findViewById(R.id.txtDistance);
		txtDistance.setTypeface(Constants.TYPEFACE_NYALA);
		txtTime = (TextView) findViewById(R.id.txtTime);
		txtTime.setTypeface(Constants.TYPEFACE_NYALA);
		txtVia = (TextView) findViewById(R.id.txtVia);
		txtVia.setTypeface(Constants.TYPEFACE_NYALA);
		TextView getDirectionFrom = (TextView) findViewById(R.id.directionListActivityFrom);
		TextView getDirectionTo = (TextView) findViewById(R.id.directionListActivityTo);
		getDirectionFrom.setTypeface(Constants.TYPEFACE_NYALA);
		getDirectionFrom.setTextColor(Color.parseColor("#336699"));
		getDirectionTo.setTypeface(Constants.TYPEFACE_NYALA);
		getDirectionTo.setTextColor(Color.parseColor("#336699"));
		listDirection = (ListView) findViewById(R.id.listDirection);
		Bundle extra = getIntent().getExtras();
		if (extra != null) {
			jsonString = extra.getString(Constants.DIRECTIONLIST);

			if (jsonString.equals("")) {
				finish();
			}
			JSONObject jsonObject;
			try {
				elocation = extra.getString(Constants.TAG_PLACES_NAME);
				jsonObject = new JSONObject(jsonString);
				txtStart.setText(jsonObject.getJSONArray(Constants.TAG_ROUTES)
						.getJSONObject(0).getJSONArray(Constants.TAG_LEGS)
						.getJSONObject(0)
						.getString(Constants.TAG_START_ADDRESS));
				if (elocation != null) {
					txtEnd.setText(elocation);
				} else {
					txtEnd.setText(jsonObject
							.getJSONArray(Constants.TAG_ROUTES)
							.getJSONObject(0).getJSONArray(Constants.TAG_LEGS)
							.getJSONObject(0)
							.getString(Constants.TAG_END_ADDRESS));
				}
				txtDistance.setText(jsonObject
						.getJSONArray(Constants.TAG_ROUTES).getJSONObject(0)
						.getJSONArray(Constants.TAG_LEGS).getJSONObject(0)
						.getJSONObject(Constants.TAG_DISTANCE)
						.getString(Constants.TAG_TEXT));
				txtTime.setText(jsonObject.getJSONArray(Constants.TAG_ROUTES)
						.getJSONObject(0).getJSONArray(Constants.TAG_LEGS)
						.getJSONObject(0).getJSONObject(Constants.TAG_DURATION)
						.getString(Constants.TAG_TEXT));
				txtVia.setText("Via	"
						+ jsonObject.getJSONArray(Constants.TAG_ROUTES)
								.getJSONObject(0)
								.getString(Constants.TAG_SUMMARY));
				JSONArray jsonArray = jsonObject
						.getJSONArray(Constants.TAG_ROUTES).getJSONObject(0)
						.getJSONArray(Constants.TAG_LEGS).getJSONObject(0)
						.getJSONArray(Constants.TAG_STEPS);
				for (int i = 0; i < jsonArray.length(); i++) {
					MoveMapDAO direction = new MoveMapDAO(null, jsonArray
							.getJSONObject(i)
							.getString(Constants.TAG_HTML_INSTRUCTIONS)
							.replaceAll("\\<.*?>", "").replaceAll("&nbsp;", "")
							.replaceAll("&amp;", ""), jsonArray
							.getJSONObject(i)
							.getJSONObject(Constants.TAG_DISTANCE)
							.getString(Constants.TAG_TEXT));
					list.add(direction);
				}
				adaptor = new ListLocalMapAdapter(getBaseContext(), list);
				listDirection.setAdapter(adaptor);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu _menu) {
		_menu.add(Menu.NONE, R.id.directionactivity_submenuitem_viewmap,
				Menu.NONE, this.getString(R.string.directionactivity_viewmap))
				.setIcon(R.drawable.ic_get_direction)
				.setShowAsAction(
						MenuItem.SHOW_AS_ACTION_IF_ROOM
								| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
		/*
		 * SubMenu subMenuSort = _menu.addSubMenu(this
		 * .getString(R.string.directionactivity_submenu_header));
		 * subMenuSort.add(Menu.NONE,
		 * R.id.directionactivity_submenuitem_viewmap, Menu.NONE,
		 * this.getString(R.string.directionactivity_viewmap))
		 * .setIcon(R.drawable.ic_get_direction);
		 * 
		 * MenuItem menuItemSort = subMenuSort.getItem();
		 * menuItemSort.setIcon(R.drawable.ic_list_w);
		 * menuItemSort.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
		 * MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		 */
		return super.onCreateOptionsMenu(_menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		case R.id.directionactivity_submenuitem_viewmap: {
			Log.v(Constants.TAG, "get view map");
			Intent intent = new Intent(DirectionListActivity.this,
					GooglemapV2Direction.class);
			intent.putExtra(Constants.DIRECTIONLIST, jsonString);
			startActivity(intent);
			return true;
		}
		default:
			return super.onOptionsItemSelected(item);
		}
	}

}
